const fs = require('fs')
const path = require('path')
const qr = require('qr-image')
const express = require('express')
const app = express()

app.set('views', path.join(__dirname, 'views'))
app.engine('.html', require('ejs').__express)
app.set('view engine', 'html')

let P = 3333
let T = '亻中 阝日'
let me = 'https://pay.liuzy88.com/'
let wx = 'https://payapp.weixin.qq.com/qr/AQGbByEumdoS5mw15yiK1e-C?t=CwAC#wechat_pay'
let ap = 'https://qr.alipay.com/tsx03078ljqhlh3twpvqca9'
let qq = 'https://i.qianbao.qq.com/wallet/sqrcode.htm?m=tenpay&a=1&u=416657468&ac=B61E692995C547783E5FB16CD7B3DB91632D1FBFF2B16A287B927C920E0A5EDB&n=liuzy&f=wallet'

let meQR = qr.imageSync(me, { size: 10 }).toString('base64')
let wxQR = qr.imageSync(wx, { size: 10 }).toString('base64')
let qqQR = qr.imageSync(qq, { size: 10 }).toString('base64')

app.use(express.static(path.join(__dirname, 'public')))
app.get('/', function(req, res, nex) {
    let UA = req.headers['user-agent']
    console.log(UA)
    if (UA.indexOf(' Alipay') !== -1) {
        res.redirect(301, ap)
    } else if (UA.indexOf(' MicroMessenger') !== -1) {
        res.render('index', { t: T, pay: wxQR, msg: '长按"识别图片中的二维码"向我付款' })
    } else if (UA.indexOf(' QQ/') !== -1) {
        res.render('index', { t: T, pay: qqQR, msg: '长按图片"扫描二维码"向我付款' })
    } else {
        res.render('index', { t: T, pay: meQR, msg: '请使用微信/支付宝/QQ扫码付款' })
    }
})

app.use(function(req, res, next) {
    res.status(404).send('404 is Fuck')
})

app.use(function(err, req, res, next) {
    console.log(err.stack)
    res.status(500).send('500 Server Error')
})

app.listen(P)

console.log('Listening on', P)